package com.celan.year2023.month02.day09;

import java.util.HashMap;
import java.util.Map;

class AuthenticationManager {
    int timeToLive;
    //<token,timeToLive>
    HashMap<String, Integer> map;

    public AuthenticationManager(int timeToLive) {
        this.timeToLive = timeToLive;
        map = new HashMap<>();
    }

    public void generate(String tokenId, int currentTime) {
        map.put(tokenId, currentTime + timeToLive);
    }

    public void renew(String tokenId, int currentTime) {
        if (!map.containsKey(tokenId)) return;
        int time = map.get(tokenId);
        if (currentTime < time) map.put(tokenId, currentTime + timeToLive);
    }

    public int countUnexpiredTokens(int currentTime) {
        int cnt = 0;
        for (int time : map.values()) {
            if (time > currentTime) cnt++;
        }
        return cnt;
    }
}